package la.iok.hzsvn.lewin.movie.operation.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.OrgUnitType;
import la.iok.hzsvn.lewin.movie.core.model.SimpleOrgUnitVo;
import la.iok.hzsvn.lewin.movie.operation.entity.SupportReturnMoneyShop;
import la.iok.hzsvn.lewin.movie.operation.mapper.SupportReturnMoneyShopMapper;
import la.iok.hzsvn.lewin.movie.operation.model.SupportReturnMoneyShopParam;
import la.iok.hzsvn.lewin.movie.operation.model.SupportReturnMoneyShopVo;
import la.iok.hzsvn.lewin.movie.services.MovieServiceInvoker;
import la.iok.hzsvn.lewin.mybatis.model.PageParam;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.lewin.mybatis.model.SortParam;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.collection.IdOperators;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SupportReturnMoneyShopServiceImpl
        extends BaseServiceImpl<SupportReturnMoneyShop, SupportReturnMoneyShopMapper,Long>
        implements SupportReturnMoneyShopService {
    private final MovieServiceInvoker movieServiceInvoker;
    public SupportReturnMoneyShopServiceImpl(SupportReturnMoneyShopMapper mapper,
                                             MovieServiceInvoker movieServiceInvoker) {
        super(mapper);
        this.movieServiceInvoker = movieServiceInvoker;
    }

    @Override
    public Long save(@NotNull Token token, Long orgUnitId) {
        token.validAnyAuthority(RoleEnum.Admin);
        ErrorCode.PROPERTY_CHECK_ERROR.assertNotNull(orgUnitId,"店铺","请选择店铺");
        SimpleOrgUnitVo orgUnit = movieServiceInvoker.orgUnitInfo(orgUnitId);
        ErrorCode.ORG_UNIT_NOT_EXIST.assertNotNull(orgUnit,orgUnitId);
        ErrorCode.PROPERTY_CHECK_ERROR.assertEqual(OrgUnitType.SHOP.getCode(), orgUnit.getType(),"组织类型","组织必须是店铺");
        SupportReturnMoneyShop entity = new SupportReturnMoneyShop();
        entity.setOrgUnitId(orgUnitId);
        insertSelective(entity);
        return entity.getId();
    }

    @Override
    public void delete(@NotNull Token token, @Nullable Long id) {
        token.validAnyAuthority(RoleEnum.Admin);
        SupportReturnMoneyShop entity = select(id);
        ErrorCode.RECORD_NOT_EXIST.assertNotNull(entity, id);
        delete(id);
    }

    @Override
    public PageVo<SupportReturnMoneyShopVo> page(Token token, PageParam pageParam, SortParam sortParam, SupportReturnMoneyShopParam listParam) {
        token.validAnyAuthority(RoleEnum.Admin);
        String orderColumn = sortParam.orderColumn("id");
        String order = sortParam.order();
        Page<SupportReturnMoneyShop> list = PageHelper.startPage(pageParam.getPage(), pageParam.getLimit());
        mapper.list(orderColumn,order,listParam);
        Set<Long> orgUintIds = list.stream().map(SupportReturnMoneyShop::getOrgUnitId).collect(Collectors.toSet());
        List<SimpleOrgUnitVo> orgUnits = movieServiceInvoker.orgUnits(orgUintIds);
        return page(list, item->{
            SupportReturnMoneyShopVo vo = new SupportReturnMoneyShopVo();
            BeanUtils.copyProperties(item, vo);
            SimpleOrgUnitVo orgUnit = IdOperators.find(orgUnits, item.getOrgUnitId());
            if(orgUnit != null){
                vo.setOrgUnitName(orgUnit.getName());
            }
            return vo;
        });
    }
}
